using System;
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;

/// <summary>
/// 基础攻击组件
/// </summary>
[Serializable]
public class AttackComponent : BaseComponent
{
    [Header("攻击设置")]
    public int attackPower = 10;    // 攻击力
    public float attackCooldown = 2f; // 攻击冷却时间
    public float nextAttackTime = 0f; // 下次可攻击时间
    public float AttackRange = 2f; //攻击范围
    public bool isAttacking;
    [ReadOnly]
    public Entity AttackTarget; //可以被攻击的物体一定是个实体且拥有生命值模块
    public Action<Entity> AttackAction; //攻击事件
    public bool IsLook; // 是否面向对方
    [HideInInspector]
    public bool IsCanAttack = true; // 是否可以攻击,从外部控制什么时候可以攻击，比如移动不能攻击之类的

    //重启攻击组件
    public virtual void Restart()
    {
        isAttacking = false;
        nextAttackTime = 0;
    }
    public override void OnUpdate()
    {
        base.OnUpdate();
        nextAttackTime += Time.deltaTime;
        if (!IsCanAttack ) {
            return;
        }
        if (entity.HasEcsComponent<HpComponent>()) {
            if (entity.GetEcsComponent<HpComponent>().IsDead) {
                return;
            }
        }
        if (AttackTarget != null) //攻击
        {
            if (AttackTarget.GetEcsComponent<HpComponent>().IsDead) //目标死亡，攻击无效
            {
                AttackTarget = null;
                if (IsLook)
                {
                    entity.GetEcsComponent<RotationComponent>().ReleaseLock(entity);
                }
                return;
                
            }
            if (Vector3.Distance(entity.transform.position, AttackTarget.transform.position) <= AttackRange)
            {
                Vector3 dir = AttackTarget.transform.position - entity.transform.position;
                dir.y = 0;

                if (IsLook)
                {
                    if (entity.HasEcsComponent<RotationComponent>())
                    {
                        entity.GetEcsComponent<RotationComponent>().TryLockAndRotateToDirection(entity, dir);
                    }
                    else
                    {
                        Debug.LogError("旋转组件缺失！");
                    }
                }
                else
                {
                    if (entity.HasEcsComponent<RotationComponent>())
                    {
                        entity.GetEcsComponent<RotationComponent>().RotateToDirection(dir);
                    }
                }
            }
            else
            {
                if (IsLook)
                {
                    entity.GetEcsComponent<RotationComponent>().ReleaseLock(entity);
                }
            }
            nextAttackTime += Time.deltaTime;
            if (nextAttackTime >= attackCooldown && Vector3.Distance(entity.transform.position, AttackTarget.transform.position) <= AttackRange && !isAttacking)
            {
                nextAttackTime = 0;
                Attack();
            }
        }
        else
        {
            if (IsLook)
            {
                entity.GetEcsComponent<RotationComponent>().ReleaseLock(entity);
            }
        }
    }

    public virtual void Attack()
    {
        if (AttackTarget.HasEcsComponent<HpComponent>())
        {
            AttackAction?.Invoke(AttackTarget);
            isAttacking = true;
        }
    }

    public virtual void ApplyDamage()
    {
        if (IsLook) {
            entity.GetEcsComponent<RotationComponent>().ReleaseLock(entity);
        }
        isAttacking = false;
        if (AttackTarget == null) //攻击成功前目标就丢失了，则攻击无效
        {
            return;
        }
        if (AttackTarget.GetEcsComponent<HpComponent>().IsDead) //攻击成功前目标就死亡，则攻击无效
        {
            return;
        }
        Vector3 tempAttackTargetPosition = AttackTarget.transform.position;
        tempAttackTargetPosition.y = entity.transform.position.y; //攻击目标的高度和攻击者一致
        if (Vector3.Distance(tempAttackTargetPosition, entity.transform.position) > AttackRange) //如果攻击目标不在攻击范围内，则攻击无效
        {
            return;
        }
        if (AttackTarget.HasEcsComponent<HpComponent>())
        {
            var hpComponent = AttackTarget.GetEcsComponent<HpComponent>();
            hpComponent.TakeDamage(attackPower, entity.gameObject);
        }
    }

}
